﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;
using NHibernate;

namespace Agr.CQRS.Infrastructure.Orm
{
    public class CriteriaQuery<T> : IOrmSessionAware
    {
        public SessionInTransaction Tx { get; set; }
        internal protected DetachedCriteria Criteria { get; set; }

        public CriteriaQuery()
        {
            Criteria = DetachedCriteria.For<T>();
        }

        internal protected ICriteria ExecutableCriteria()
        {
            return Criteria.GetExecutableCriteria(Tx.Session);
        }

        public virtual IList<T> Results()
        {
            return ExecutableCriteria().List<T>();
        }
        public virtual T Result()
        {
            Criteria.SetMaxResults(1);
            return Results().FirstOrDefault();
        }

    }
}
